﻿<%@ page language="java" import="com.newcrm.bean.*,com.newcrm.util.Util,com.newcrm.config.*,java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
  User user = (User) session.getAttribute("user");
  Map<Integer, CompanyInfo> companys = (Map<Integer, CompanyInfo>) application.getAttribute(Const.APPLICATION_COMPANYS);
  CompanyInfo companyInfo = companys.get(Integer.valueOf(user.companyId));
  List<Map<String, Object>> customerCategorys = companyInfo.customerCategorys;
  List<User> users = companyInfo.users;

  String companyId = (String)request.getAttribute("companyId");
  if (companyId == null) {
    companyId = "";
  }
  String companyName = (String)request.getAttribute("companyName");
  if (companyName == null) {
    companyName = "";
  }
  String phone = request.getParameter("phone");
  if (phone == null) {
    phone = "";
  }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>添加客户 - 添加联系人</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="renderer" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/qikebao.ico" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/all.css?ver=<%=Const.VERSION%>">
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/datepicker.css" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrap-datetimepicker.min.css" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/magnific-popup.css">
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-1.11.1.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-ui.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.bootstrap-growl.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/moment.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap-datepicker.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap-datetimepicker.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/daterangepicker.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.magnific-popup.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.cookie.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/popbox.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/visiblecommon.js?ver=<%=Const.VERSION%>"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/commonCustomer_new.js?ver=<%=Const.VERSION%>"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/companySearch.js?ver=<%=Const.VERSION%>"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/area.js?ver=<%=Const.VERSION%>"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/common.js?ver=<%=Const.VERSION%>"></script>
  <script>
    var customerCategorys = new Array();
    var _share_type_user = '<%=Const.SHARE_TYPE_USER%>';
    var _share_type_group = '<%=Const.SHARE_TYPE_GROUP%>';
    var _visible_person = '<%=Const.VISIBLE_PERSON%>';
    <%if (customerCategorys != null) {
        for (Map<String, Object> category:customerCategorys) {
          String color = (String) category.get("Color");
          if (color == null || color.trim().length() == 0) {
            color = "000000";
          }%>
    customerCategorys.push({id:"<%=category.get("CategoryId")%>",name:"<%=Util.UEncode((String)category.get("CategoryName"))%>", color:"<%=color%>"});
      <%}
    } %>
    var allUsers = new Array();
    <%if (users != null) {
        for (User u:users) { %>
    allUsers.push({id:"<%=u.userId%>",name:"<%=Util.UEncode(u.name)%>",photo:"<%=u.imgPath%>"});
    <%  }
      } %>
    $(function() {
      if ($("#customer_phone").val() != "") {
        $("#customer_phone_title").hide();
        $("#customer_phone_container").show();
      }
      $("#peopleName").focus();
      $("#add_link").click(function(e) {
        e.preventDefault();
        $('#newForm').submit();
      });
      $('#newForm').submit(function(event){
        if ($("#peopleName").val() == "") {
          alert("请输入联系人姓名");
          $("#peopleName").focus();
          return false;
        }

        var _tel = $("#phone_show input").map(function() {return $(this).val();
        }).get().join();
        if (_tel != "") {
          var _check = checkTelExist(_tel);
          if (_check != 1) {
<%  if (user.isAdmin || user.isGroupAdmin) {%>
            var r = confirm("您提交的电话号码与以下客户重复：\r" + _check + " \r 是否继续提交新客户？");
            if (!r) {
              return false;
            }
<% } else { %>
            alert("您提交的电话号码与以下客户重复，无法新创建。\r" + _check);
            return false;
<% }%>
          }
        }

        var _qq = $("#im_show input").map(function() {return $(this).val();
        }).get().join();
        if (_qq != "") {
          var _check = checkQQExist(_qq);
          if (_check != 1) {
<%  if (user.isAdmin || user.isGroupAdmin) {%>
            var r=confirm("您提交的QQ与以下客户重复：\r" + _check + " \r 是否继续提交新客户？");
            if (!r) {
              return false;
            }
<% } else { %>
            alert("您提交的QQ与以下客户重复，无法新创建。\r" + _check);
            return false;
<% }%>
          }
        }

        var emailinputError = false;
        $("input[name='email']").each(function (i) {
          if (!checkEmail($(this).val())) {
            emailinputError = true;
            alert("邮箱格式不正确");
            $(this).focus();
          }
        });
        if (emailinputError) {
          return false;
        }
        
        return true;
      });
      $("#companyName").autocomplete({
        source: "<%=request.getContextPath()%>/companyName?operation=list",
        select: function(event, ui ) {
          var _id = ui.item ? ui.item.id : "";
          $("#companyId").val(_id);
        }
      }).data("ui-autocomplete")._renderItem = function(ul, item) {
        return $( "<li></li>" )
          .data( "ui-autocomplete-item", item)
          .append("<a>" + item.label + "</a>")
          .appendTo(ul);
      };
    });
  </script>
</head>
<body class="parties">
  <jsp:include page="topNavi.jsp" flush="true"/>
  <div class="main-page">
    <table>
    <tr>
      <td class="main-body">
        <div class="topnavi">
          <div class='leftMenu'>
            <%
            String disabled = "";
            String returnUrl = "";
            if (companyId.length() > 0) { %>
            <span>添加 [<%=Util.HTMLEncode(companyName)%>] 的联系人</span>
            <%
              disabled = "disabled";
              returnUrl = request.getContextPath() + "/company/" + companyId + "#people";
            } else {%>
            <span>添加联系人</span> <span class="s"> 或 <a href="<%=request.getContextPath()%>/company/new?phone=<%=phone%>">添加公司</a></span>
            <%}%>
          </div>
        </div>
        <div class="edit-page">
          <form action="<%=request.getContextPath()%>/people" autocomplete="off" method="post" id="newForm">
            <input type="hidden" name="operation" value="new">
            <input type="hidden" name="returnUrl" value="<%=returnUrl%>">
            <div class="page_divider">
              <div class="primary_fields">
              <table cellpadding="0" cellspacing="0" class="contact_types">
                <tr class="name">
                  <th><h2>姓名</h2></th>
                  <td><input class="name autofocus" id="peopleName" name="peopleName" size="30" type="text" /> <span>(必填)</span></td>
                </tr>
                <tr class="name">
                  <th><h2>职位</h2></th>
                  <td><input class="autofocus" id="peopleTitle" name="peopleTitle" size="30" type="text" /></td>
                </tr>
                <tr class="name">
                  <th><h2>公司</h2></th>
                  <%if (companyId.length() > 0) {%>
                  <td><input class="autofocus" id="companyName" name="companyName2" size="30" type="text" disabled value="<%=Util.HTMLEncode(companyName)%>" />
                    <input id="companyId" name="companyId" type="hidden" value="<%=companyId%>" />
                    <input id="companyId" name="companyName" type="hidden" value="<%=Util.HTMLEncode(companyName)%>" />
                  </td>
                  <%} else {%>
                  <td><input class="autofocus" id="companyName" name="companyName" size="30" type="text" value="<%=Util.HTMLEncode(companyName)%>" />
                    <input id="companyId" name="companyId" type="hidden" value="<%=companyId%>" />
                  </td>
                  <%}%>
                </tr>
              </table>
              </div>
            </div>
            <jsp:include page="commonCustomer_new.jsp" flush="true">
              <jsp:param name="customerType" value="people"/>
              <jsp:param name="companyId" value="<%=companyId%>"/>
            </jsp:include>
            <p class="submit">
               <a id="add_link" class="button blue" href="#"><i class="fa fa-check"></i> 添加联系人</a>
              <a class="button grey" href="<%=request.getContextPath()%>/parties"><i class="fa fa-times"></i> 取消</a>
            </p>
          </form>
        </div>
      </td>
      <td class="main-sidebar">
        <div class="content">
          <h2 class="clearfix">可以方便地导入/导出客户信息</h2>
          <ul>
            <li><a href="<%=request.getContextPath()%>/customerimports/new"><i class="fa fa-file-excel-o"></i> 导入Excel/CSV文件</a></li>
          </ul>
          <h2>撞单查询</h2>
          <div class="top5"><a href="#search-popup" class="open-popup-link button small grey"><i class="fa fa-search"></i> 撞单查询</a></div>
        </div>
      </td>
    </tr>
    </table>
  </div>

  <div id="search-popup" class="white-popup big mfp-hide">
    <div class="popup-title"><span class="title">撞单查询</span> <span class="popup-close-button"><i class="fa fa-times"></i>&nbsp;</span></div>
    <div class="popup-content">
      <div class="advsearch">
        <p class="title">检索系统中的客户信息，避免撞单</p>
        <form id="search_customers" method="post" >
          <table class="searchDetail">
            <tr>
              <td>名称</td><td><input class="autofocus" type="text" value="" id="txtCompanyName"></td>
              <td>电话</td><td><input class="autofocus" type="text" value="" id="txtTel"></td>
            </tr>
            <tr>
              <td>邮箱</td><td><input class="autofocus" type="text" value="" id="txtEmail"></td>
              <td>QQ</td><td><input class="autofocus" type="text" value="" id="txtQQ">
              <span id="span_submit"><a id="search-link" class="button blue small" href="#"><i class="fa fa-search"></i> 搜索</a> <span id="noresult_msg">没检索到符合条件的结果</span></span>
              </td>
            </tr>
          </table>
        </form>
        <table class="searchresult" id="contactsList"></table>
      </div>
    </div>
  </div>
</body>
</html>